using UnityEngine;
using System.Collections;
using System.Collections.Generic;

// To use this class, add the following:
//
// Make some code run on a specific type of event:
//
// 		Listeners.inst().add(Channel.ON_WEAPON_SWAP, 
//			delegate(Entity e) {
//				// Code here
//			}
//		);
//
//
// Broadcast to all listeners on a specific channel that an event occured:
//
// 		Listeners.inst().notify(Channel.ON_WEAPON_SWAP, entity);
//

public enum Channel {
	ON_WEAPON_SWAP,			// Activated multiplayer.
	ON_BARREL_FIRE,			// Activated multiplayer.
	ON_SPUDGUN_FIRE,		// Activated multiplayer.
	ON_ENTERED_BUILDSCREEN,	// Activated singleplayer.
	ON_LEFT_BUILDSCREEN,	// Activated singleplayer.
	ON_BUILD_LOCKED,		// Activated singleplayer.
	ON_LOOT_ENTITY,			// Activated singleplayer.
	ON_CONSUME_ENTITY,		// Activated singleplayer.
	ON_ENTITY_SPAWNED,		// Activated multiplayer.
	ON_ENTITY_DESTROYED,	// Activated multiplayer.
	ON_ENTITY_DROPPED,		// Activated multiplayer.
	ON_DAMAGE,				// Activated multiplayer.
	ON_AREA_BASH,			// Activated multiplayer.
	ON_MELEE,				// Activated multiplayer.
	ON_JUMP,				// Activated multiplayer.
	ON_GROUNDED,			// Activated multiplayer.
	ON_FIRE,				// Activated multiplayer.
	ON_WON_GAME,
	ON_LOST_GAME,
	ON_FIGHT,
	ON_TURNED_NIGHT,
	ON_TURNED_DAY,
	
	// Will probably be removed later on.
	ON_LAND,
	ON_RUN,
	ON_SPUDGUN_PROJECTILE_LANDING,
	ON_COLLIDE_BEER,
	ON_COLLIDE_BUSH,
	ON_USED_TAPE,
	ON_CLICKED_STARTMENU,
	ON_STUNNED,
	ON_OVERENCUMBERED
	
}

public class Listeners {
	private Dictionary<Channel, List<System.Action<PlayerState>>> listeners;
	
	// Singleton pattern.
	private static Listeners instance;
	public static Listeners inst() {
		if (instance == null) instance = new Listeners();
		return instance;
	}
	
	// Constructor.
	private Listeners() {
		listeners = new Dictionary<Channel, List<System.Action<PlayerState>>>();
	}
	
	// Add a listener on a specific channel.
	public void add(Channel channel, System.Action<PlayerState> listener) {
		if (!listeners.ContainsKey(channel)) {
			listeners.Add(channel, new List<System.Action<PlayerState>>());
		}
		
		listeners[channel].Add(listener);
	}
	
	// Notify listeners on a specific channel.
	public void notify(Channel channel, PlayerState player) {
		if (listeners.ContainsKey(channel)) {
			foreach (System.Action<PlayerState> listener in listeners[channel]) {
				listener(player);
			}
		}
	}
}